﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Metadata.W3cXsd2001;

namespace Com.Framework.Enc
{
    public enum ByteEncodeMethod
    {
        None,
        Hex,
        Base64
    }

    public class ByteArrayEncoder
    {
        public static string EncodeHex(byte[] ByteArray)
        {
            SoapHexBinary shb = new SoapHexBinary(ByteArray);
            return shb.ToString();
        }

        public static byte[] DecodeHex(string Encoded)
        {
            SoapHexBinary shb = SoapHexBinary.Parse(Encoded);
            return shb.Value;
        }

        public static string EncodeBase64(byte[] ByteArray)
        {
            return Convert.ToBase64String(ByteArray);
        }

        public static byte[] DecodeBase64(string Encoded)
        {
            return Convert.FromBase64String(Encoded);
        }

        public static string Encode(byte[] ByteArray, ByteEncodeMethod Method)
        {
            string retVal = string.Empty;
            switch (Method)
            {
                case ByteEncodeMethod.Base64:
                    retVal = EncodeBase64(ByteArray);
                    break;
                case ByteEncodeMethod.Hex:
                    retVal = EncodeHex(ByteArray);
                    break;
                default:
                    retVal = string.Empty;
                    break;
            }

            return retVal;
        }

        public static byte[] Decode(string Encoded, ByteEncodeMethod Method)
        {
            byte[] retVal = null;

            switch (Method)
            {
                case ByteEncodeMethod.Base64:
                    retVal = DecodeBase64(Encoded);
                    break;
                case ByteEncodeMethod.Hex:
                    retVal = DecodeHex(Encoded);
                    break;
                default:
                    retVal = null;
                    break;
            }

            return retVal;
        }
    }
}
